DESANTI = { };
( function( D )
{
    D.request = { };

	var selectedAction;

	var oldFillInfoBubble = Timeline.DefaultEventSource.Event.prototype.fillInfoBubble;
	Timeline.DefaultEventSource.Event.prototype.fillInfoBubble = function(elmt, theme, labeller) 
	{
		oldFillInfoBubble.call(this, elmt, theme, labeller);

	    var eventObject = this;
		selectedAction = eventObject.actionId;
		
		D.request( "trace3.txt", { "actionId": selectedAction }, function( results )
		{
			if( results.length > 0 )
			{
				nicEditors.findEditor( "wysiwyg" ).setContent( decodeURIComponent( results[ 0 ].txt.value ) );
			}
			else
			{
				nicEditors.findEditor( "wysiwyg" ).setContent( "" );
			}
		});		
	}
	
	var init = function()
	{
		var t1,
           	resizeTimerID;

		nicEditors.allTextAreas( );

		document.getElementById( "saveButton" ).onclick = function()
		{
			var txt = encodeURIComponent( nicEditors.findEditor( "wysiwyg" ).getContent( ) );
			D.request( "trace4.txt", { "actionId": selectedAction }, function( )
			{
				D.request( "trace2.txt", 
				{ 	"actionId": selectedAction, 
					"txt": txt ,
					"user": D.userName(),
					"timestamp": new Date().getTime()
				}, null, true );
			}, true );
		}

       D.request( "trace.txt", { }, function( results )
	   {
		var eventSource = new Timeline.DefaultEventSource(),
	   		timestamp,
	   		time,
	   		arg;

	   	var makeDescription = function( result )
	   	{
	   		arg.description += "<div class=\"line\"><span class=\"label\">" + 
	   			decodeURIComponent( result.withArgument.value ) + "</span>";
	   		if( result.argPicture )
	   		{
	   			arg.description += "<span class=\"value\">" +
	   				"<img class=\"picture\" src=\"" + result.argPicture.value + "\"/></span></div>";
	   		}
	   		else if( results[i].argLabel )
	   		{
	   			arg.description += "<span class=\"value\">" + decodeURIComponent( result.argLabel.value ) +"</span></div>";
	   		}
	   		else
	   		{
	   			arg.description += "<span class=\"value\">" + decodeURIComponent( result.argURI.value ) +"</span></div>";
	   		}
	   	}

	   	for( var i = 0 ; i < results.length ; i += 1 )
	   	{
	   		if( timestamp === results[ i ].timestamp.value )
	   		{
	   			makeDescription( results[ i ] );
	   		}
	   		else
	   		{
	   			if( arg )
	   			{
	   				var evt = new Timeline.DefaultEventSource.Event( arg );
					evt.actionId = arg.actionId;
	               	eventSource.add(evt);
	   			}	

	   			timestamp = results[ i ].timestamp.value;
	   			time = new Date( parseInt( timestamp ) );
	   			arg = { };
				arg.actionId = results[i].action.value;
	   			arg.start = time;
	            arg.instant = true;
	            arg.text = results[ i ].userName.value + ": " + results[ i ].actionLabel.value;
	   			arg.description = "";
	   			makeDescription( results[ i ] );
	   		}
	   	}

	   	if( results.length > 1)
	   	{
	   		var evt = new Timeline.DefaultEventSource.Event( arg );
			evt.actionId = arg.actionId;	
           	eventSource.add(evt);
	   	}

           var bandInfos = [
             Timeline.createBandInfo(
                {
                    width:          "40%", 
                    intervalUnit:   Timeline.DateTime.MINUTE, 
                    intervalPixels: 50,
                    eventSource: eventSource
                } ),
             Timeline.createBandInfo(
                {
                    width:          "35%", 
                    intervalUnit:   Timeline.DateTime.HOUR, 
                    intervalPixels: 100,
                    eventSource: eventSource
                } ),
             Timeline.createBandInfo(
                {
                    width:          "25%", 
                    intervalUnit:   Timeline.DateTime.DAY, 
                    intervalPixels: 200,
                    eventSource: eventSource
                } ) ];

           bandInfos[1].syncWith = 0;
           bandInfos[1].highlight = true;
           bandInfos[2].syncWith = 1;
           bandInfos[2].highlight = true;

           tl = Timeline.create( document.getElementById( "timeline" ), bandInfos );

	   } );

       window.onresize = function( ) 
       {
          if( resizeTimerID == null ) 
          {
              resizeTimerID = window.setTimeout( function( ) 
	   		{
	   			resizeTimerID = null;
                   tl.layout( );
               }, 500 );
          }
       }
	}

    window.onload = function( )
    {
		if( self.opener && self.opener.DESANTI )
		{
			D.userName = self.opener.DESANTI.userName;
			init( );
		}
		else
		{
			alert("Vous n'êtes pas connectés");
		}
    }

} )( DESANTI );
